<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>角色维护界面</title>
    <link rel="stylesheet" href="../../res/layui/css/layui.css" media="all">

    <script src="../../res/layui/layui.js" charset="utf-8"></script>
    <script src="../../res/js/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="../../res/js/tools.js" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

    <script type="text/javascript">
        $.ajaxSetup({
                async:false
            })
    </script>
</head>
<body>
<form class="layui-form" action="" lay-filter="example">

    <input type="hidden" name="roleId" id="roleId">
    <!-- 角色名称-->
    <div class="layui-form-item">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-block">
            <input type="text" name="roleName" id="roleName" lay-verify="title" autocomplete="off" placeholder="请输入名称" class="layui-input">
        </div>
    </div>


    <!-- 备注 -->
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <input type="text" name="roleRemark" id="roleRemark" lay-verify="title"
                   autocomplete="off" placeholder="备注" class="layui-input">
        </div>
    </div>


    <!--<div class="layui-form-item">
        <label class="layui-form-label">是否有效</label>
        <div class="layui-input-block">
            <input type="checkbox" name="roleValidity" checked=""
                   id="roleValidity" lay-skin="switch" lay-text="有效|无效">
        </div>
    </div>-->

    <div class="layui-form-item">
        <label class="layui-form-label">负责模块：</label>

    </div>
    <div id="test7" class="demo-tree"></div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit=""
                    lay-filter="demo1">立即提交</button>
        </div>
    </div>

</form>

<script>
    layui.use(['tree','form', 'layedit', 'laydate','upload','util'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate
            ,upload = layui.upload
            ,tree = layui.tree
            ,util = layui.util;


        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');
        //自定义验证规则
        form.verify({
            title: function(value){
                if(value.length < 2){
                    return '标题至少得2个字符啊';
                }
            }
            ,pass: [
                /^[\S]{6,12}$/
                ,'密码必须6到12位，且不能出现空格'
            ]
            ,content: function(value){
                layedit.sync(editIndex);
            }
        });
        //监听提交
        form.on('submit(demo1)', function(data){
            var url = "../../role/RoleInsert.form";
            var id = "";
            $("input[name='layuiTreeCheck']:checked").each(function(index,items){
                id+=$(this).val()+",";
            });
            data.field.moduleId = id ;
            $.post(url,data.field,function(obj){
                if(obj.state==1){
                    layer.msg(obj.msg,{
                        icon:1,
                        time:1000,
                    },function(){
                        var indexs = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(indexs);
                    });
                    return false ;
                }
            },"json")
            return false;
        });
        var id = getQueryString("id");

        if(id!=null){
            alert(id);
            //开启复选框
            var url = '../../role/findByAllThree.form';
            var data = {id:id}
            $.post(url,data,function(obj){
                console.info(obj)
                tree.render({
                    elem: '#test7'
                    ,data: obj
                    ,showCheckbox: true
                });
            },'json')
            var url = "../../role/RoleById.form";
            var data = {id:id};
            $.post(url,data,function(obj){
               // alert(obj.roleId);

                $("#roleId").val(obj.roleId);
                $("#roleName").val(obj.roleName);
                $("#roleRemark").val(obj.roleRemark);
            },'json')
        }else{
           // alert("hhh");
            var url = '../../role/findByAllTwo.form';

            $.post(url,null,function(obj){

                tree.render({
                    elem: '#test7'
                    ,data: obj
                    ,showCheckbox: true
                });
            },'json')
        }
    });
</script>
</body>
</html>